//
//  UserWorkListViewController.m
//  RedGrass
//
//  Created by 徐良俊 on 2019/3/18.
//  Copyright © 2019 hongcaosp. All rights reserved.
//

#import "UserWorkListViewController.h"
#import "RGMeVedioCell.h"
#import "HoverViewFlowLayout.h"
#import <AFNetworking/AFNetworking.h>
#import "RGMeInterfaceBulider.h"
#import "LoadMoreControl.h"

@interface UserWorkListViewController()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView                 *collectionView;
@property (nonatomic, assign) CGFloat                          itemWidth;
@property (nonatomic, assign) CGFloat                          itemHeight;
@property (nonatomic, assign) NSInteger                        pageIndex;

@property (nonatomic, strong) LoadMoreControl                  *loadMore;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic, copy) void(^listScrollViewScrollBlock)(UIScrollView *scrollView);
@end

@implementation UserWorkListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _itemWidth = (KScreenWidth - (CGFloat)(((NSInteger)(KScreenWidth)) % 3) ) / 3.0f - 1.0f;
    _itemHeight = _itemWidth * 1.35f;
    _dataArray = [NSMutableArray new];
    [self.view addSubview:self.collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    _loadMore = [[LoadMoreControl alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 50) surplusCount:15];
//    [_loadMore startLoading];
//    [self loadData:self.userId loadMore:NO];
    __weak __typeof(self) wself = self;
    [_loadMore setOnLoad:^{
        [wself loadData:wself.userId loadMore:YES];
    }];
    [_collectionView addSubview:_loadMore];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RGMeVedioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RGMeVedioCell" forIndexPath:indexPath];
    if (self.tabIndex == 0) {
        RGRecommendVideoModel *model = [self.dataArray objectAtIndex:indexPath.row];
        cell.model = model;
        cell.showCount = model.playCount;
        cell.isShowLikeImg = NO;
    } else {
        RGRecommendVideoModel *model = [self.dataArray objectAtIndex:indexPath.row];
        cell.model = model;
        cell.showCount = model.likeCounts;
        cell.isShowLikeImg = YES;
    }
    return cell;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray ? self.dataArray.count : 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return  CGSizeMake(_itemWidth, _itemHeight);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if([self.userViewDelegate respondsToSelector:@selector(cellItemChosedWithView:dataList:chosedTabIndex:)]){
        [self.userViewDelegate cellItemChosedWithView:indexPath dataList:_dataArray chosedTabIndex:_tabIndex];
    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView  alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            //            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[RGMeVedioCell class] forCellWithReuseIdentifier:@"RGMeVedioCell"];
    }
    return _collectionView;
}

-(void)refreshData {
    [self.loadMore reset];
    [self.loadMore startLoading];
    [self loadData:self.userId loadMore:NO];
}

- (void)loadData:(NSInteger)userId loadMore:(Boolean)loadMore {
    AA_WEAK_MAKE(self) weakSelf = self;
    if (loadMore) {
        _pageIndex++;
    } else {
        _pageIndex = 1;
    }
    HttpRequest *quest;
    NSDictionary *params;
    if(_tabIndex == 0) {
        quest = [HttpRequest buildRequest:url_userWorks];
        params = [RGMeInterfaceBulider buildUserWorks:userId WithPageNumber:self.pageIndex pageSize:PageSize];
    } else {
        quest = [HttpRequest buildRequest:url_userLoves];
        params = [RGMeInterfaceBulider buildUserLoves:userId WithPageNumber:self.pageIndex pageSize:PageSize];
    }
    [quest post:params success:^(id data) {
        weakSelf.pageIndex ++;
        if (!loadMore) {
            weakSelf.dataArray = [NSMutableArray new];
        }
        NSArray *array = [RGRecommendVideoModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
        [weakSelf.dataArray addObjectsFromArray:array];
        [weakSelf.collectionView reloadData];
        [weakSelf.loadMore endLoading];
        if(array.count < PageSize) {
            [weakSelf.loadMore loadingAll];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.loadMore loadingFailed];
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.listScrollViewScrollBlock ? : self.listScrollViewScrollBlock(scrollView);
}

#pragma mark - GKPageListViewDelegate
- (UIScrollView *)listScrollView {
    return self.collectionView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView * _Nonnull))callback {
    self.listScrollViewScrollBlock = callback;
}

@end
